﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.Model;
using FantastPlatform.Framework.Service;
using FantastPlatform.Community.IService;
using FantastPlatform.Framework.Core;
using NHibernate;

namespace FantastPlatform.Community.Service
{
    public class NoticeService : DataService<INotice, Notice>, INoticeService
    {
        #region INoticeService 成员

        public IList<INotice> GetNoticeListByThisWeek()
        {
            ConditionGroup conditionGroup = new ConditionGroup();
            DateTime? startTime = getWeekStartTime();
            DateTime? EndTime = DateTime.Now;
            Condition enableCondition = new Condition() { Type = typeof(bool).FullName, Field = "Enable", Value1 = true };
            Condition dateCondition = new Condition() { Type = typeof(DateTime).FullName, Field = "CreateDate", Value1 = startTime,Value2=EndTime, Comparison = Comparisons.Between };         
            conditionGroup.Conditions.Add(enableCondition);
            conditionGroup.Conditions.Add(dateCondition);
            IList<Sort> sorts = new List<Sort>();
            int totalCount = 0;
            sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Asc });            
            return base.Search(conditionGroup, sorts, Int32.MaxValue, 1, out totalCount);

        }
        private DateTime? getWeekStartTime()
        {
            DateTime now = DateTime.Now;
            DayOfWeek weekDay = now.DayOfWeek;
            if (weekDay == 0)
                weekDay = DayOfWeek.Sunday;
            DateTime startTime = now.AddDays(-Convert.ToInt32(weekDay) + 1).Date;
            return startTime;
        }
        public override void Update(INotice entity)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tran) =>
            {

                entity.ModifyDate = DateTime.Now;
                entity.Enable = true;
                Session.Merge(entity);
            }, transaction);
        }
        #endregion
    }
}
